<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgpu - FXAA</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="example.css">
	</head>

	<body>

		<div id="info" class="invert">
			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>

			<div class="title-wrapper">
				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>FXAA</span>
			</div>

			<small>Fast Approximate Anti-Aliasing.</small>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/webgpu": "../build/three.webgpu.js",
					"three/tsl": "../build/three.tsl.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three/webgpu';
			import { pass, renderOutput } from 'three/tsl';
			import { fxaa } from 'three/addons/tsl/display/FXAANode.js';

			import { Inspector } from 'three/addons/inspector/Inspector.js';

			const params = {
				enabled: true,
				animated: false
			};

			let camera, scene, renderer, clock, group;
			let postProcessing;

			init();

			async function init() {

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
				camera.position.z = 50;

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0xffffff );

				clock = new THREE.Clock();

				//

				const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d );
				hemiLight.position.set( 0, 1000, 0 );
				scene.add( hemiLight );

				const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
				dirLight.position.set( - 3000, 1000, - 1000 );
				scene.add( dirLight );

				//

				group = new THREE.Group();

				const geometry = new THREE.TetrahedronGeometry();
				const material = new THREE.MeshStandardMaterial( { color: 0xf73232, flatShading: true } );

				const mesh = new THREE.InstancedMesh( geometry, material, 100 );
				const dummy = new THREE.Object3D();

				for ( let i = 0; i < 100; i ++ ) {

					dummy.position.x = Math.random() * 50 - 25;
					dummy.position.y = Math.random() * 50 - 25;
					dummy.position.z = Math.random() * 50 - 25;

					dummy.scale.setScalar( Math.random() * 2 + 1 );

					dummy.rotation.x = Math.random() * Math.PI;
					dummy.rotation.y = Math.random() * Math.PI;
					dummy.rotation.z = Math.random() * Math.PI;

					dummy.updateMatrix();
					mesh.setMatrixAt( i, dummy.matrix );

				}

				group.add( mesh );
				scene.add( group );

				renderer = new THREE.WebGPURenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( animate );
				renderer.inspector = new Inspector();
				document.body.appendChild( renderer.domElement );

				// post processing

				postProcessing = new THREE.PostProcessing( renderer );

				// ignore default output color transform ( toneMapping and outputColorSpace )
				// use renderOutput() for control the sequence

				postProcessing.outputColorTransform = false;

				// scene pass

				const scenePass = pass( scene, camera ).toInspector( 'Color' );
				const outputPass = renderOutput( scenePass );

				// FXAA must be computed in sRGB color space (so after tone mapping and color space conversion)

				const fxaaPass = fxaa( outputPass );
				postProcessing.outputNode = fxaaPass;

				//

				window.addEventListener( 'resize', onWindowResize );

				//

				const gui = renderer.inspector.createParameters( 'Settings' );
				gui.add( params, 'enabled' ).onChange( ( value ) => {
			
					if ( value === true ) {

						postProcessing.outputNode = fxaaPass;

					} else {

						postProcessing.outputNode = outputPass;

					}

					postProcessing.needsUpdate = true;

				} );
				gui.add( params, 'animated' );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animate() {

				const delta = clock.getDelta();

				if ( params.animated === true ) {

					group.rotation.y += delta * 0.1;

				}

				postProcessing.render();

			}

		</script>

	</body>
</html>
